package com.qdu.bussiness.service.impl;

import java.util.List;

import com.qdu.bussiness.constant.BussinessConstant;
import com.qdu.bussiness.domain.BusInventory;
import com.qdu.bussiness.service.IBusInventoryService;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.utils.DateUtils;
import com.qdu.common.utils.SecurityUtils;
import com.qdu.common.utils.id.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdu.bussiness.mapper.BusEntryMapper;
import com.qdu.bussiness.domain.BusEntry;
import com.qdu.bussiness.service.IBusEntryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库记录Service业务层处理
 * 
 * @author qdu
 * @date 2022-03-05
 */
@Service
public class BusEntryServiceImpl implements IBusEntryService 
{
    @Autowired
    private BusEntryMapper busEntryMapper;

    @Autowired
    private IBusInventoryService busInventoryService;

    /**
     * 查询入库记录
     * 
     * @param entryId 入库记录主键
     * @return 入库记录
     */
    @Override
    public BusEntry selectBusEntryByEntryId(Long entryId)
    {
        return busEntryMapper.selectBusEntryByEntryId(entryId);
    }

    /**
     * 查询入库记录列表
     * 
     * @param busEntry 入库记录
     * @return 入库记录
     */
    @Override
    public List<BusEntry> selectBusEntryList(BusEntry busEntry)
    {
        return busEntryMapper.selectBusEntryList(busEntry);
    }

    /**
     * 新增入库记录
     * 
     * @param busEntry 入库记录
     * @return 结果
     */
    @Override
    public AjaxResult insertBusEntry(BusEntry busEntry)
    {
        // 校验物料编号是否重复
        if(checkItemNoExist(busEntry.getItemNo())){
            return AjaxResult.error("当前物料编号已经存在，请更换后重试！");
        }
        busEntry.setEntryId(IdUtil.getId());
        busEntry.setCreateBy(SecurityUtils.getUsername());
        busEntry.setCreateTime(DateUtils.getNowDate());
        busEntry.setEntryTime(DateUtils.getNowDate());
        busEntry.setEntryStatus(BussinessConstant.ENTRY_STATUS_NEW);
        int res = busEntryMapper.insertBusEntry(busEntry);
        return res > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 校验物料编号是否重复 - 针对新增
     * @param itemNo
     * @return
     */
    private boolean checkItemNoExist(String itemNo) {
        return busEntryMapper.checkItemNoExist(itemNo) > 0;
    }

    /**
     * 校验物料编号是否重复 - 针对修改
     * @param itemNo
     * @return
     */
    private boolean checkItemNoExist(String itemNo, Long entryId) {
        BusEntry busEntry = busEntryMapper.selectBusEntryByItemNo(itemNo);
        if(busEntry == null){
            return false;
        }else if(busEntry.getEntryId().equals(entryId)){
            return false;
        }
        return true;
    }

    /**
     * 修改入库记录
     * 
     * @param busEntry 入库记录
     * @return 结果
     */
    @Override
    public AjaxResult updateBusEntry(BusEntry busEntry)
    {
        // 校验物料编号是否重复
        if(checkItemNoExist(busEntry.getItemNo(), busEntry.getEntryId())){
            return AjaxResult.error("当前物料编号已经存在，请更换后重试！");
        }
        busEntry.setUpdateBy(SecurityUtils.getUsername());
        busEntry.setUpdateTime(DateUtils.getNowDate());
        int res = busEntryMapper.updateBusEntry(busEntry);
        return res > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除入库记录
     * 
     * @param entryIds 需要删除的入库记录主键
     * @return 结果
     */
    @Override
    public int deleteBusEntryByEntryIds(Long[] entryIds)
    {
        return busEntryMapper.deleteBusEntryByEntryIds(entryIds);
    }

    /**
     * 删除入库记录信息
     * 
     * @param entryId 入库记录主键
     * @return 结果
     */
    @Override
    public int deleteBusEntryByEntryId(Long entryId)
    {
        return busEntryMapper.deleteBusEntryByEntryId(entryId);
    }

    /**
     * 修改入库单状态
     * @param entryId
     * @param entryStauts
     * @return
     */
    @Override
    public AjaxResult changeEntryStatus(Long entryId, String entryStauts) {
        if(busEntryMapper.updateEntryStatus(entryId, entryStauts) > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 确认入库
     * @param entryId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult ensureEntry(Long entryId) {
        // 修改入库单状态为 入库
        if(busEntryMapper.updateEntryStatus(entryId, BussinessConstant.ENTRY_STATUS_SUBMIT) < 1){
            return AjaxResult.error("确认入库失败！尚未查询到当前入库单，请刷新列表后重试！");
        }
        BusEntry busEntry = busEntryMapper.selectBusEntryByEntryId(entryId);
        // 生成库存记录
        BusInventory busInventory = new BusInventory();
        busInventory.setItemId(busEntry.getItemId());
        busInventory.setItemNo(busEntry.getItemNo());
        busInventory.setStorageRoom(busEntry.getStorageRoom());
        busInventory.setStorageArea(busEntry.getStorageArea());
        busInventory.setStoragePosition(busEntry.getStoragePosition());
        busInventory.setInventoryStatus(BussinessConstant.INVENTORY_STATUS_NORMAL);
        busInventory.setItemNum(busEntry.getEntryNum());
        if(busInventoryService.insertBusInventory(busInventory) < 1){
            throw new RuntimeException("确认入库失败！");
        }
        return AjaxResult.success();
    }
}
